package com.nuc.kl.module.usermanager.action;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.nuc.kl.module.usermanager.entity.Student;
import com.nuc.kl.module.usermanager.entity.Teacher;
import com.nuc.kl.module.usermanager.entity.User;
import com.nuc.kl.module.usermanager.service.StudentService;
import com.nuc.kl.module.usermanager.service.TeacherService;
import com.nuc.kl.module.usermanager.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


@Controller
@Scope("prototype")
public class PrivateAction extends ActionSupport{

	private Long id;
	private String name;
	private String loginName;
	private String password;
	private int gender;
	private String phoneNumber;
	private String email;
	
	private String school;
	private int isExpert;
	private String stuNum;
	private String entryYear;
	
	@Resource
	private UserService userService;
	@Resource
	private TeacherService teacherService;
	@Resource
	private StudentService studentService;
	
	
	
	public String list() throws Exception{
		
		return "list";
	}
	
	public String update() throws Exception{
		String md5Digest = DigestUtils.md5Hex(password);
		Object obj = ActionContext.getContext().getSession().get("people");
		int peopleFlag = (Integer) ActionContext.getContext().getSession().get("peopleFlag");
		
		if(peopleFlag==1){ //管理员
			User user = (User)obj;
			user.setName(name);
			user.setPassword(md5Digest);
			user.setPhoneNumber(phoneNumber);
			user.setEmail(email);
			
			userService.update(user);
		}else if(peopleFlag==2){//教师
			Teacher teacher = (Teacher)obj;
			teacher.setName(name);
			teacher.setPassword(md5Digest);
			teacher.setSchool(school);
			
		}else if(peopleFlag==3){//学生
			Student student = (Student)obj;
			student.setName(name);
			student.setPassword(md5Digest);
			student.setStuNum(stuNum);
			student.setEntryYear(entryYear);
		}
		
		return "login";
		
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getGender() {
		return gender;
	}

	public void setGender(int gender) {
		this.gender = gender;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}

	public int getIsExpert() {
		return isExpert;
	}

	public void setIsExpert(int isExpert) {
		this.isExpert = isExpert;
	}

	public String getStuNum() {
		return stuNum;
	}

	public void setStuNum(String stuNum) {
		this.stuNum = stuNum;
	}

	public String getEntryYear() {
		return entryYear;
	}

	public void setEntryYear(String entryYear) {
		this.entryYear = entryYear;
	}
	
	
	
}
